package com.example.online_order.conrtoller;

import com.example.online_order.pojo.Result;
import com.example.online_order.pojo.Shoppingcar;
import com.example.online_order.pojo.Shoucanglan;
import com.example.online_order.service.ShoppingcarService;
import com.example.online_order.service.ShoucanglanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/shoucanglan")
@CrossOrigin
@Slf4j
public class ShoucanglanController {
    @Autowired
    ShoucanglanService shoucanglanService;
    @GetMapping("/getAllS")
    public Result getAllS(){
        return new Result(shoucanglanService.getAllS());
    }

    @GetMapping("/getSById/{id}")
    public Result getSById(@PathVariable("id") Integer id){
        return new Result(shoucanglanService.getSById(id));
    }

    @GetMapping("/getSByUsername/{userName}")
    public Result getSByUserName(@PathVariable("userName") String userName){
        return new Result(shoucanglanService.getSByUsername(userName));
    }

    @PostMapping("/addToS")
    public Result addToS(@RequestBody Shoucanglan s){
        Integer res = shoucanglanService.addToS(s);
        if (res != 1 ){
            return  Result.fail("failed",500);
        }else{
            return Result.success("success");
        }
    }
    @DeleteMapping("/delS/{id}")
    public Result deleteS(@PathVariable("id") Integer id){
        Integer res = shoucanglanService.deleteS(id);
        if (res == 1){
            return new Result("delete success");
        }else{
            return  Result.fail("delete failed",444);
        }
    }

    @PutMapping("/updateS")
    public Result updateS(@RequestBody Shoucanglan s){
        Integer res = shoucanglanService.updateS(s);
        if (res == 1){
            return new Result("update success");
        } else{
            return  Result.fail("update failed",444);
        }
    }

    @GetMapping("/getLike/{goodsname}")
    public Result getByGoodsName(@PathVariable("goodsName") String goodsname){
        return  new Result(shoucanglanService.findSLikeGoodsname(goodsname));
    }
//    增删改查（名称模糊查询）
}
